import request from '../../uitls/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList:[],//banner数据
        articleList:[],//文章列表
        topArticleList:[],//置顶文章
        pageIndex:0,//文章列表分页
        isTriggered:false,// 标识下拉刷新是否被触发
    },

    // 获取轮播相关数据
    async getBannerListData(){
        let bannerListData = await request("/banner/json");
        this.setData({
            bannerList:bannerListData.data,
        })
    },

    //获取置顶文章
    async getTopArticleListData(){

        let topArticleListData = await request("/article/top/json");
        topArticleListData = topArticleListData.data;
        topArticleListData.forEach((value,index) =>{
            value['isTop'] = true
        })
        this.setData({
            topArticleList:topArticleListData
        })

         
        let {topArticleList} = this.data;
        let {articleList} = this.data;


        topArticleList = topArticleList.reverse();
        topArticleList.forEach(element => {
                articleList.unshift(element);
        }); 


        this.setData({
            articleList
        })
        wx.hideLoading()


    },

    // 获取文章列表数据
    async getArticleListData(index){
        let articleListData = await request('/article/list/'+index+'/json');

        this.setData({
            articleList:articleListData.data.datas,
        })

        this.getTopArticleListData();
        
    },

    //列表下拉刷新回调
    async handleRefresher(){

        let {articleList} = this.data;

        let articleListData =  await request('/article/list/0/json');
        let newList = articleListData.data.datas.reverse();

        newList.forEach(element => {
            let articleItem = articleList.find(item => item.id === element.id);
            if(articleItem == null)
            {
                articleList.unshift(element);
            }
        });       

        this.setData({
            articleList
        });

        wx.stopPullDownRefresh();

       

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({
          title: '正在加载',
        })
        this.getBannerListData();
        this.getArticleListData(this.data.pageIndex);
    },

    // 跳转至详情页面
    toDetail(event){
        let deskUrl = event.currentTarget.dataset.url;
        wx.navigateTo({
          url: '/pages/webpage/webpage?url='+deskUrl,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.handleRefresher();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        wx.showLoading({
          title: '正在加载',
        });
        this.loadMoreArticleList();
    },

    async loadMoreArticleList(){
        let {pageIndex} = this.data;
        let {articleList} = this.data;

        let articleListData = await request('/article/list/'+ (++pageIndex)+'/json')
        let newList = articleListData.data.datas;

        newList.forEach(element => {
            let articleItem = articleList.find(item => item.id === element.id);
            if(articleItem == null)
            {
                articleList.push(element);
            }
        });       

        wx.hideLoading();

        this.setData({
            articleList,
            pageIndex
        });

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})